package com.xy.breakfast.controller;

import com.xy.breakfast.entity.Foods;
import com.xy.breakfast.service.FoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//食品控制器
@Controller
public class FoodsController {

    @Autowired
    FoodsService foodsService;


    //获取食品（参数以get方式直接获取）
    @RequestMapping("/getGoodsById")
    @ResponseBody
    public Foods getGoodsById(int id){
        return foodsService.getById(id);
    }

    //获取所有食品
    @RequestMapping("/getAllFoods")
    @ResponseBody
    public List<Foods> getAllFoods(){
        return foodsService.getAll();
    }

    //获取20个食品的库存进行数据可视化
    @RequestMapping("/getFoodsNum")
    @ResponseBody
    public Map<String,Object> getFoodsNum(){
        //查询食品
        List<Foods> foodsList=foodsService.getAll();

        //返回结果
        Map<String,Object> result=new HashMap<>();

        //设置x,y数据
        List<String> stringList=new ArrayList<>();
        List<Integer> numList=new ArrayList<>();
        for(Foods foods:foodsList){
            stringList.add(foods.getName());
            numList.add(foods.getNum());
        }

        //把数据存在map中
        result.put("x",stringList);
        result.put("y",numList);

        //返回
        return result;
    }
    //按照类型获取食品
    @RequestMapping("/getFoodsByTypesId")
    @ResponseBody
    public List<Foods>getFoodsByTypesId(@RequestBody Map<String,String>map){
        int typesId=Integer.valueOf(map.get("typesId"));
        return foodsService.getByTypesId(typesId);
    }


    @RequestMapping("/getFoodsSalePrice")
    @ResponseBody
    public List<Map<String,Object>>getFoodsSalePrice(){
        return foodsService.getFoodsSalePrice();
    }

    //当天不同类型的销售额
    @RequestMapping("/getFoodsByTypesIdDay")
    @ResponseBody
    public List<Map<String, String>> getFoodsByTypesId() {
        return foodsService.getFoodsByTypesIdDay();
    }

    //    不同类型销售比例图
    @RequestMapping("/getFoodsByNum")
    @ResponseBody
    public List<Map<String, Object>> getFoodsByNum() {
        return foodsService.getFoodsByNum();
    }

    //按名字查询

    @RequestMapping("/getBooksByName")
    @ResponseBody
    public List<Foods> getBooksByName(@RequestBody Map<String,String> map) {
        String name=map.get("name");
        return foodsService.getBooksByName(name);
    }
}
